package fudan.se.project.service.infrastructure.base;


import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.Conference;
import fudan.se.project.domain.conference.role.Author;
import fudan.se.project.domain.conference.role.Chair;
import fudan.se.project.domain.conference.role.PcMember;
import fudan.se.project.repository.entity.conference.ConferenceRepository;
import fudan.se.project.repository.entity.conference.role.AuthorRepository;
import fudan.se.project.repository.entity.conference.role.ChairRepository;
import fudan.se.project.repository.entity.conference.role.PcMemberRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ConferencePermissionService {

    private final ChairRepository chairRepository;
    private final PcMemberRepository pcMemberRepository;
    private final AuthorRepository authorRepository;

    private final ConferenceRepository conferenceRepository;

    private static final String CHAIR_AUTH = "CHAIR";
    private static final String PC_MEMBER_AUTH = "PCMEMBER";
    private static final String AUTHOR_AUTH = "AUTHOR";

    public ConferencePermissionService(ChairRepository chairRepository, PcMemberRepository pcMemberRepository, AuthorRepository authorRepository, ConferenceRepository conferenceRepository) {
        this.chairRepository = chairRepository;
        this.pcMemberRepository = pcMemberRepository;
        this.authorRepository = authorRepository;
        this.conferenceRepository = conferenceRepository;
    }

    public boolean checkPermission(User user, String authority, long resourceId) {
        Conference conference = conferenceRepository.findById(resourceId);
        if (conference == null) return false;

        if (CHAIR_AUTH.equals(authority)) {
            return null != chairRepository.findByUserAndConference(user, conference);
        } else if (PC_MEMBER_AUTH.equals(authority)) {
            return null != pcMemberRepository.findByUserAndConference(user, conference);
        } else if (AUTHOR_AUTH.equals(authority)) {
            return null != authorRepository.findByUserAndConference(user, conference);
        }

        return false;
    }

    public boolean checkPcMemberPermission(User user, Conference conference) {
        return this.checkPermission(user, PC_MEMBER_AUTH, conference.getId());
    }

    public List<String> fetchRoles(User user, Conference conference) {
        ArrayList<String> arrayList = new ArrayList<>();

        if (user == null) return arrayList;

        Chair chair = chairRepository.findByUserAndConference(user, conference);
        PcMember pcMember = pcMemberRepository.findByUserAndConference(user, conference);
        Author author = authorRepository.findByUserAndConference(user, conference);

        if (chair != null) {
            arrayList.add(CHAIR_AUTH);
        }
        if (pcMember != null) {
            arrayList.add(PC_MEMBER_AUTH);
        }
        if (author != null) {
            arrayList.add(AUTHOR_AUTH);
        }
        return arrayList;
    }
}
